package com.ophiux.FeverManagement.aplication.serviceImpl.patient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.FeverManagement.aplication.bean.patient.AddPatientInfoResponse;
import com.ophiux.FeverManagement.aplication.bean.patient.EditPatientInfoRequest;
import com.ophiux.FeverManagement.aplication.bean.patient.EditPatientInfoResponse;
import com.ophiux.FeverManagement.aplication.service.patient.IEditPatientInfoService;
import com.ophiux.FeverManagement.domain.entity.RbPatientinfo;
import com.ophiux.FeverManagement.domain.repository.IPatientRepositories;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.common.DateUtil;

import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class EditPatientInfoServiceImpl implements IEditPatientInfoService{
	@Autowired
	IPatientRepositories patientRepositories;//患者信息仓储
	
	@Override
	public Result<EditPatientInfoResponse> editPatientInfo(EditPatientInfoRequest dto) {
		Result<EditPatientInfoResponse> result = new Result<>();
		if(!dto.isParamValid()) {
			return Result.fail(ResultEnum.requestParamFail);
		}
		try {
			/**
			 * 查询要修改的患者信息是否存在
			 */
			RbPatientinfo patientinfo = patientRepositories.selectOneByKey(dto.getPATIENT_ID());
			if(patientinfo == null) {//该患者不存在档案信息  不允许修改
				return Result.fail(ResultEnum.NotFoundPatient);
			}
			
			/**
			 * 查询将要修改的患者信息是否存在
			 */
			RbPatientinfo queryIdnoPatient = patientRepositories.getPatientByIdNo(dto.getID_NO());
			if(queryIdnoPatient != null) {//修改的身份证已经存在档案信息
				if(!queryIdnoPatient.getID_NO().equals(patientinfo.getID_NO())) {//并且不是修改的这个患者本人的信息
					//将要修改为数据库已经存在该身份证的患者  不允许修改
					return Result.fail(ResultEnum.PATIENT_EXIST);
					
				}
			}
			
			
			
			
			
			patientinfo.setADDRESS(dto.getADDRESS());
			patientinfo.setOPERATION_TIME(DateUtil.getCurDateStr());
			patientinfo.setID_NO(dto.getID_NO());
			patientinfo.setPATIENT_NAME(dto.getPATIENT_NAME());
			patientinfo.setTEL(dto.getTEL());
			String patientId = patientRepositories.save(patientinfo);
			
			EditPatientInfoResponse response = new EditPatientInfoResponse();
			response.setRESULT("1");
			result = Result.success(response);
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.fail("","操作异常");
		}
		return result;
	}

}
